package com.xigege.netty.dubborpc.netty;

import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;


import java.lang.reflect.Proxy;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class NettyClient {
    //创建线程池
    private static ExecutorService executor = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

    //客户端的handler
    private static NettyClientHandler clientHandler;

    //编写一个方法使用代理模式，获取一个代理对象
    //参数一：将来要代理的service类，HelloServiceImpl
    //参数二：将来跟服务器通讯的时候，协议头是什么，helloServer#hello#xxxx
    public Object getBean(final Class<?> serviceClass,final String providerName ){
        return Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(),new Class<?>[]{serviceClass},
                //proxy 代理对象，method 调用的方法，args 调方法时传的参数
                //这个lambda 的代码，客户端每调用一次hello()，就会调用一次该代码
                (proxy,method,args)->{
                    if (clientHandler == null){
                        initClient();
                    }
                    //设置要发给服务器端的设置
                    //providerName 协议头，args[0]就是客户端调用api “hello(??)” 传进来的参数
                    clientHandler.setPara(providerName + args[0]);

                    return executor.submit(clientHandler).get();
                });
    }

    //初始化客户端
    private static void initClient(){
        clientHandler = new NettyClientHandler();
        //创建NioEventLoopGroup
        NioEventLoopGroup eventExecutors = new NioEventLoopGroup();

        Bootstrap bootstrap = new Bootstrap();
        bootstrap.group(eventExecutors)
                .channel(NioSocketChannel.class)
                .option(ChannelOption.TCP_NODELAY,true)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        pipeline.addLast(new StringDecoder());
                        pipeline.addLast(new StringEncoder());
                        pipeline.addLast(clientHandler);
                    }
                });
        try {
            bootstrap.connect("127.0.0.1",7000).sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
